package XComponent.hide
{
	import fl.controls.Button;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	public class XPage extends EventDispatcher
	{
		static public const PAGE_CHANGED : String = "PAGE_CHANGED";	
		
		private var m_btnPageDown : DisplayObject;
		private var m_btnPageUp   : DisplayObject;
		
		private var m_totalCount : int;		// 总数
		private var m_totalPage : 	int;		// 总页数
		private var m_curPage   :	int;		// 当前页
		private var m_showPageCount : int;		// 每页的显示数
		
		/**
		 * 
		 * @param pageUpBtn			上一页控制元件
		 * @param pageDownBtn		先一页控制元件
		 * @param totalCount		实体总个数
		 * @param showPageCount		每页显示的个数
		 * 
		 */		
		public function XPage(pageUpBtn:DisplayObject=null,pageDownBtn:DisplayObject=null,totalCount:int=0,showPageCount:int=12)
		{
			m_totalCount = 			totalCount;
			m_btnPageUp = 			pageUpBtn;
			m_btnPageDown = 		pageDownBtn;
			m_showPageCount = 		showPageCount;
			m_curPage =				1;
			
			calculateTotalPage();
			
			if(m_btnPageUp)
			{
				m_btnPageUp.addEventListener(MouseEvent.CLICK,OnClickPageUpBtn);
			}
			if(m_btnPageDown)
			{
				m_btnPageDown.addEventListener(MouseEvent.CLICK,OnClickPageDownBtn);
			}
		}
		
		public function destory() : void
		{
			if(m_btnPageUp)
			{
				m_btnPageUp.removeEventListener(MouseEvent.CLICK,OnClickPageUpBtn);
			}
			if(m_btnPageDown)
			{
				m_btnPageDown.removeEventListener(MouseEvent.CLICK,OnClickPageDownBtn);
			}
			
			 m_btnPageDown		 	= 	null;
			 m_btnPageUp  		 	= 	null;
			
			 m_totalCount			=   0;
			 m_totalPage		 	= 	0;
			 m_curPage  		 	=	0;
			 m_showPageCount 		= 	0;
		}
		
		/**
		 *跳到某页 
		 * @param page
		 * 
		 */		
		public function gotoPage(page : int) : void
		{
			if(page > 0 && page < m_totalPage)
			{
				m_curPage = page;
				this.dispatchEvent(new Event(PAGE_CHANGED));
			}
		}
		
		/**
		 *	当前页 
		 * @return 
		 * 
		 */		
		public function get curPage() : int
		{
			return m_curPage;
		}
		
		/**
		 * @return  	当前页显示的个数  实际看到的个数
		 */		
		public function get viewCount() : int
		{
			var lastCount:int = m_totalCount - (m_curPage - 1)*m_showPageCount;
			
			if(lastCount > m_showPageCount)
			{
				return m_showPageCount;
			}
			else
			{
				return lastCount;
			}
			
			return 0;
		}
		
		/**
		 * 总页数
		 * @return 总页数
		 * 
		 */		
		public function get totalPage() : int
		{
			return m_totalPage;
		}
		
		/**
		 * 
		 * @return 展示个数
		 * 
		 */		
		public function get showCount() : int
		{
			return m_showPageCount;
		}
		
		/**
		 * 当前页的开始索引
		 * @return 当前页的开始索引
		 * 
		 */		
		public function get curBeginIndex() : int
		{
			return (m_curPage - 1)*m_showPageCount;
		}
		
		/**
		 * 当前页的结束索引
		 * @return 当前页的结束索引 
		 * 
		 */		
		public function get curEndIndex() : int
		{
			return curBeginIndex + viewCount;
		}
		
		private function OnClickPageDownBtn(evt : MouseEvent):void
		{
			pageDown();
		}
		
		private function OnClickPageUpBtn(evt : MouseEvent):void
		{
			pageUp();
		}
		
		/**
		 *下一页 
		 * 
		 */		
		private function pageDown() : void
		{
			if(m_curPage + 1 <= m_totalPage)
			{
				m_curPage++;
				this.dispatchEvent(new Event(PAGE_CHANGED));
			}
		}
		
		/**
		 *上一页 
		 * 
		 */		
		private function pageUp() : void
		{
			if(m_curPage - 1 > 0)
			{
				m_curPage--;
				this.dispatchEvent(new Event(PAGE_CHANGED));
			}
		}
		
		/**
		 *计算总页数 
		 * 
		 */		
		private function calculateTotalPage() : void
		{
			if(m_totalCount < m_showPageCount)
			{
				m_totalPage = 1;
			}
			else if(m_totalCount%m_showPageCount)
			{
				m_totalPage = m_totalCount/m_showPageCount + 1;	
			}
			else
			{
				m_totalPage = m_totalCount/m_showPageCount;
			}
		}
	}
}